using System;
using System.Linq;
using CommandSystem;
using Exiled.API.Features;

namespace CustomCharacter.Commands.CustomCharacterCommands
{
    public class Set : ICommand
    {
        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.ForceclassWithoutRestrictions))
            {
                response = "你没有权限使用此命令！";
                return false;
            }

            if (arguments.Count < 2)
            {
                response = "用法: customrole set 玩家ID 角色类名";
                return false;
            }

            if (!int.TryParse(arguments.At(0), out int id))
            {
                response = "请输入一个正确的数字！";
                return false;
            }

            if (!Player.TryGet(id, out var player))
            {
                response = "无法找到指定的玩家！";
                return false;
            }

            Type type =
                CustomCharacterPlugin.instance.Manager.AllTypes.Keys.FirstOrDefault(x => x.Name == arguments.At(1));
            if (type is null)
            {
                response = "无法找到指定的物品，请输入customrole list查询";
                return false;
            }

            CustomCharacter.SpawnPlayer(player, type);
            response = "已给予玩家指定角色";
            return true;
        }

        public string Command { get; } = "set";
        public string[] Aliases { get; } = Array.Empty<string>();
        public string Description { get; } = "设置玩家的插件角色";
    }
}